package me.imli.app.util;

import me.imli.app.App;
import me.imli.app.R;
import me.imli.app.widget.crouton.Crouton;
import me.imli.app.widget.crouton.Style;
import android.app.Activity;
import android.content.Context;
import android.widget.Toast;

public class ToastUtil {
	
	/**
	 * TAG
	 */
	public static final String TAG = "ToastUtil";

	private static Toast mToast = null;

	/**
	 * 
	 * @param resId or msg
	 */
    public static void showShort(int resId) {
    	showShort(getString(resId));
    }
    public static void showShort(String message) {
    	show(App.getContext(), message, Toast.LENGTH_SHORT);
    }

    /**
     * 
	 * @param resId or msg
     */
    public static void showLong(int resId) {
    	showLong(getString(resId));
    }
    public static void showLong(String message) {
    	show(App.getContext(), message, Toast.LENGTH_LONG);
    }
    
    /**
     * 
     * @param context
     * @param resId
     * @param duration
     */
    public static void show(Context context, int resId, int duration) {
    	show(context, getString(resId), duration);
    }
    public static void show(Context context, String message, int duration) {
    	if (context != null && message != null) {
    		if (mToast != null) mToast.cancel();
            mToast = Toast.makeText(context, message, duration);
            mToast.show();
    	} else {
    		LogUtil.e(TAG, R.string.null_);
    	}
    }
    
    /**
     * 自定义Crouton提示
     */
    public static void showShort(Activity activity, int resid,int viewGrougResid) {
        showShort(activity, resid, null, viewGrougResid);
    }

    public static void showShort(Activity activity, int resid, Style style, int viewGrougResid) {
    	try {
            if (style == null) {
                style = Style.ALERT;
            }
            Crouton.makeText(activity, App.getContext().getString(resid), style, viewGrougResid).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    public static void showShort(Activity activity, String msg, int viewGrougResid) {
        showShort(activity, msg, null, viewGrougResid);
    }

    public static void showShort(Activity activity, String msg, Style style, int viewGrougResid) {
    	try {
        	if (msg == null) {
        		return;
        	}
            if (style == null) {
                style = Style.ALERT;
            }
            Crouton.makeText(activity, msg, style, viewGrougResid).show();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
	/**
	 * getString
	 * @return
	 */
	private static String getString(int resId) {
		try {
			return App.getContext().getString(resId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
